$(function () {
    $("#accordion").accordion({
        collapsible: true
    });

    $(".product-box-get-info").click(function () {
        $("#loading-box").dialog("open");
        var $elem = $(this);
        var $product_id = $($elem).children().filter("#product_id").val();

        getProductInfo($product_id);
    });

    $('.add-product').click(function () {
        var $product_stock = $('#product_stock');

        var current_count = $($product_stock).val();

        current_count = parseInt(current_count);

        if (current_count < 99) {
            current_count += 1;
        }

        $($product_stock).val(current_count);
    });

    $('.minus-product').click(function () {
        var $product_stock = $('#product_stock');

        var current_count = $($product_stock).val();
        var $product_current_count = $('#product_info_current_stock').text();

        current_count = parseInt(current_count);
        $product_current_count = parseInt($product_current_count);

        if (current_count < 0) {
            if (Math.abs(current_count) < $product_current_count) {
                current_count -= 1;
            }
        } else {
            current_count -= 1;
        }

        $($product_stock).val(current_count);
    });

    $('#update_product').click(function () {
        $("#confirmation-message").text("Are you sure you want to save?");
        $("#confirmation-box").dialog({
            buttons: {
                "Yes": function () {
                    $(this).dialog("close");
                    saveProduct();
                },
                "No": function () {
                    $(this).dialog("close");
                }
            }
        });
        $("#confirmation-box").dialog("open");
    });

    getProductInfo(20001);

});

function showName(prodId) {
    hideName();
    $("#prodName_" + prodId).slideDown();
}

function hideName() {
    $(".product-image-name").slideUp();
}

function getProductInfo(product_id) {
    var request = {
        'product_id': product_id
    };

    var url = getUrl('product', 'getProductInfo');

    $.post(url, request, function (response) {
        var obj = jQuery.parseJSON(response);

        $('#product_info_id').val(obj.product_id);
        $('#product_info_name').text(obj.product_name);
        $('#product_info_current_stock').text(obj.product_current_count);
        $('#product_info_image').attr('src', obj.product_image);
        $("#loading-box").dialog("close");
    });
}

function saveProduct() {
    $("#loading-box").dialog("open");
    var $product_id = $('#product_info_id').val();
    var $product_qty = $('#product_stock').val();
    var $product_current_count = $('#product_info_current_stock').text();

    $product_qty = parseInt($product_qty);
    $product_current_count = parseInt($product_current_count);

    var total = $product_current_count + $product_qty;

    if ($product_id > 0 && total >= 0) {
        var request = {
            'product_id': $product_id,
            'product_qty': total
        };

        var url = getUrl('product', 'updateQty');

        $.post(url, request, function () {

            getProductInfo($product_id);

            $('#product_stock').val('00');
        });
    }

    $("#loading-box").dialog("close");
}